use inequality_data_70s_correct , clear 

set more off

*drop years for which we do not have all observations
drop if ref_year==1979
drop if ref_year==2013

*ensure summation in Total Vehicle Cons works correctly
foreach var of varlist vehicpq vehiccq cartknpq cartkncq cartkupq cartkucq caropq carocq {
replace `var' = 0 if (`var'==. & ref_year>=1980) 
}

*Generate component totals to create different measures of consumption
* food at home
gen fdhome_tot = .
replace fdhome_tot = fdhome/4 if (ref_year<1970)
replace fdhome_tot = d_fdhome/4 if (ref_year>=1970 & ref_year<=1979) 
replace fdhome_tot = fdhomepq2+fdhomecq2 if (ref_year >= 1980)
*transportation 
gen trans_tot = .
replace trans_tot = (publictransother+automobileops+n_tot_vflow1)/4 if(ref_year<1970) 
replace trans_tot = (d_gas + d_othvehic-d_autofin+d_pubtrans+n_tot_vflow1)/4 if (ref_year>=1970 & ref_year<=1979)
replace trans_tot = (transpq+transcq+n_tot_vflow1-(vehfinpq+vehfincq+vehicpq+vehiccq+cartknpq+cartkncq+cartkupq+cartkucq+caropq+carocq)) if (ref_year>=1980)
*utilities 
gen util_tot = .
replace util_tot = utilites/4 if (ref_year<1970) 
replace util_tot = d_utility/4 if (ref_year>=1970 & ref_year<=1979) 
replace util_tot = utilpq+utilcq if (ref_year>=1980) 
*Adjusted utilities from predictions
*replace util_tot = n_adj_util_floor if (cutenure==4 & _merge_rnt==3)
*housing
gen hous_tot = . 
replace hous_tot = (totrentdwe +hflow)/4 if (ref_year<1970) 
replace hous_tot = (d_rent + hflow)/4 if (ref_year>=1970 & ref_year<=1979) 
replace hous_tot = rendwepq+rendwecq+hflow if (ref_year>=1980) 
*difference in adjusted and reported utilities is utility payment included in rent (forced to be positive) 
*gen diff_rnt = n_adj_util_floor - (utilpq+utilcq) if (cutenure==4 & _merge_rnt==3) 
*replace hous_tot = hous_tot - diff_rnt if (cutenure==4 & _merge_rnt==3 & diff_rnt>0 & diff_rnt!=.) 
*vehicles 
gen vehic_tot = . 
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*Recode cons5 to drop all elements of transportation that are not well-measured
*AND to get rid of public housing from measure
rename cons5 uncorrected_cons
gen double cons5 = .
replace cons5= ((uncorrected_cons*scale)+(n_tot_vflow1/4)+gas_mo-trans_tot)/scale if (ref_year<1980)
replace cons5=((uncorrected_cons*scale)+n_tot_vflow1+gas_mo-trans_tot)/scale if (ref_year>=1980)
replace cons5= ((cons5*scale)-(tflow2-tflow))/scale if (ref_year>=1980) 

*Generate cpi defalator
gen cpi_u_rs_adj_11=.
replace cpi_u_rs_adj_11=4.263 if (ref_year==1960)
replace cpi_u_rs_adj_11=4.266 if (ref_year==1961)
replace cpi_u_rs_adj_11=4.27 if (ref_year==1962)
replace cpi_u_rs_adj_11=4.259 if (ref_year==1963)
replace cpi_u_rs_adj_11=4.248 if (ref_year==1964)
replace cpi_u_rs_adj_11=4.228 if (ref_year==1965)
replace cpi_u_rs_adj_11=4.156 if (ref_year==1966)
replace cpi_u_rs_adj_11=4.075 if (ref_year==1967)
replace cpi_u_rs_adj_11=3.962 if (ref_year==1968)
replace cpi_u_rs_adj_11=3.834 if (ref_year==1969)
replace cpi_u_rs_adj_11=3.693 if (ref_year==1970)
replace cpi_u_rs_adj_11=3.575 if (ref_year==1971)
replace cpi_u_rs_adj_11=3.509 if (ref_year==1972)
replace cpi_u_rs_adj_11=3.337 if (ref_year==1973)
replace cpi_u_rs_adj_11=3.064 if (ref_year==1974)
replace cpi_u_rs_adj_11=2.86 if (ref_year==1975)
replace cpi_u_rs_adj_11=2.733 if (ref_year==1976) 
replace cpi_u_rs_adj_11=2.597 if (ref_year==1977)
replace cpi_u_rs_adj_11=2.449 if (ref_year==1978)
replace cpi_u_rs_adj_11=2.252 if (ref_year==1979)
replace cpi_u_rs_adj_11=2.041 if (ref_year==1980) 
replace cpi_u_rs_adj_11=1.878 if (ref_year==1981)
replace cpi_u_rs_adj_11=1.784 if (ref_year==1982)
replace cpi_u_rs_adj_11=1.724 if (ref_year==1983)
replace cpi_u_rs_adj_11=1.669 if (ref_year==1984)
replace cpi_u_rs_adj_11=1.627 if (ref_year==1985)
replace cpi_u_rs_adj_11=1.61 if (ref_year==1986)
replace cpi_u_rs_adj_11=1.57 if (ref_year==1987) 
replace cpi_u_rs_adj_11=1.526 if (ref_year==1988)
replace cpi_u_rs_adj_11=1.474 if (ref_year==1989)
replace cpi_u_rs_adj_11=1.415 if (ref_year==1990)
replace cpi_u_rs_adj_11=1.377 if (ref_year==1991)
replace cpi_u_rs_adj_11=1.353 if (ref_year==1992)
replace cpi_u_rs_adj_11=1.331 if (ref_year==1993)
replace cpi_u_rs_adj_11=1.313 if (ref_year==1994)
replace cpi_u_rs_adj_11=1.293 if (ref_year==1995)
replace cpi_u_rs_adj_11=1.269 if (ref_year==1996)
replace cpi_u_rs_adj_11=1.252 if (ref_year==1997)
replace cpi_u_rs_adj_11=1.245 if (ref_year==1998)
replace cpi_u_rs_adj_11=1.229 if (ref_year==1999) 
replace cpi_u_rs_adj_11=1.198 if (ref_year==2000)
replace cpi_u_rs_adj_11=1.175 if (ref_year==2001)
replace cpi_u_rs_adj_11=1.165 if (ref_year==2002)
replace cpi_u_rs_adj_11=1.149 if (ref_year==2003)
replace cpi_u_rs_adj_11=1.127 if (ref_year==2004)
replace cpi_u_rs_adj_11=1.099 if (ref_year==2005)
replace cpi_u_rs_adj_11=1.073 if (ref_year==2006)
replace cpi_u_rs_adj_11=1.051 if (ref_year==2007)
replace cpi_u_rs_adj_11=1.02 if (ref_year==2008) 
replace cpi_u_rs_adj_11=1.032 if (ref_year==2009) 
replace cpi_u_rs_adj_11=1.024 if (ref_year==2010) 
replace cpi_u_rs_adj_11=1 if (ref_year==2011)
replace cpi_u_rs_adj_11=.987 if (ref_year==2012)

****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale 

*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}

 *collapse components on 10th 50th and 90th percentiles, by year
 preserve 
 collapse (p10) p10trans=r_trans_scle p10hous=r_hous_scle p10fdhome=r_fdhome_scle p10hous_util=r_hous_util_scle p10vehic=r_vehic_scle (p50) medtrans=r_trans_scle medhous=r_hous_scle medfdhome=r_fdhome_scle medhous_util=r_hous_util_scle medvehic=r_vehic_scle (p90) p90trans=r_trans_scle p90hous=r_hous_scle p90fdhome=r_fdhome_scle p90hous_util=r_hous_util_scle p90vehic=r_vehic_scle [w=wgt20] , by(ref_year)
 *generate ratios 
 foreach x in "trans" "hous" "fdhome" "hous_util" "vehic" {
 gen ratio`x'_90_10 = p90`x'/p10`x'
 gen ratio`x'_90_50 = p90`x'/med`x'
 gen ratio`x'_50_10 = med`x'/p10`x'
 }
 
*Export to excel for cleaning up and presentation 
export excel using component_pctiles.xls , sh(comp_old) firstrow(var) replace 
 
restore 

*Create indicators of percentile of core consumption by year AND for food at home cons
foreach year of numlist 1972 1973 1983/2012 {
foreach num of numlist 5 15 45 55 85 95 {
egen cons5pct_`num'_`year' = pctile(cons5) if (ref_year==`year') , p(`num') 
egen fdhomepct_`num'_`year' = pctile(fdhome_scle) if (ref_year==`year') , p(`num')
}
}
gen str12 indicator=""
foreach year of numlist 1972 1973 1983/2012 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & cons5>=cons5pct_5_`year' & cons5<=cons5pct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & cons5>=cons5pct_45_`year' & cons5<=cons5pct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & cons5>=cons5pct_85_`year' & cons5<=cons5pct_95_`year')
}
gen str12 ind_fdhome=""
foreach year of numlist 1972 1973 1983/2012 {
replace ind_fdhome= "pct_10_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_5_`year' & fdhome_scle<=fdhomepct_15_`year')
replace ind_fdhome= "pct_50_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_45_`year' & fdhome_scle<=fdhomepct_55_`year')
replace ind_fdhome= "pct_90_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_85_`year' & fdhome_scle<=fdhomepct_95_`year')
}
drop cons5pct_*
drop fdhomepct_*

*Restrict sample to observations in a given year between percentiles 5 and 15, 45 and 55, and 85 and 95
tempfile temp10 temp50 

*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.) 

foreach year of numlist 1972 1973 1983/2012 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save percentile_means_`year' , replace 
restore 

preserve 
keep if ind_fdhome=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=50
save "`temp50'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=90

append using `temp50'
append using `temp10'
save fdpct_means_`year' , replace 
restore
}

use percentile_means_1972 , clear 
foreach year of numlist 1973 1983/2012 {
append using percentile_means_`year' 
}
save percentile_means_allyears , replace 

use fdpct_means_1972 , clear
foreach year of numlist 1973 1983/2012 { 
append using fdpct_means_`year'
}
save fdpct_means_allyears , replace 

*REPEAT THE CREATION OF COMPONENT PERCENTILES USING THE PREDICTIONS FROM QREGRESSIONS FOR 1961 AND 1980-81
foreach y in 60 80 {
use inequality_data_for_`y' , clear 
drop if (ref_year==1972 | ref_year==1973 | ref_year==1983 | ref_year==1984)
****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot
*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale

*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}

*collapse components on 10th 50th and 90th percentiles, by year
preserve 
collapse (p10) p10trans=r_trans_scle p10hous=r_hous_scle p10fdhome=r_fdhome_scle p10hous_util=r_hous_util_scle p10vehic=r_vehic_scle (p50) medtrans=r_trans_scle medhous=r_hous_scle medfdhome=r_fdhome_scle medhous_util=r_hous_util_scle medvehic=r_vehic_scle (p90) p90trans=r_trans_scle p90hous=r_hous_scle p90fdhome=r_fdhome_scle p90hous_util=r_hous_util_scle p90vehic=r_vehic_scle [w=wgt20] , by(ref_year)
*generate ratios 
foreach x in "trans" "hous" "fdhome" "hous_util" "vehic" {
gen ratio`x'_90_10 = p90`x'/p10`x'
gen ratio`x'_90_50 = p90`x'/med`x'
gen ratio`x'_50_10 = med`x'/p10`x'
}
 
*Export to excel for cleaning up and presentation 
export excel using component_pctiles.xls , sh(comp_pred`y') firstrow(var)  
restore
}
 
use inequality_data_for_60 , clear 
drop if (ref_year==1972 | ref_year==1973)

****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot
*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1979)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale


*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}
 
*Create indicators of percentile of core consumption by year
foreach num of numlist 5 15 45 55 85 95 {
egen cons5pct_`num'_1961 = pctile(cons5) if (ref_year==1961) , p(`num')
egen fdhomepct_`num'_1961 = pctile(fdhome_scle) if (ref_year==1961) , p(`num') 
}

gen str12 indicator=""
replace indicator = "pct_10_1961" if (ref_year==1961 & cons5>=cons5pct_5_1961 & cons5<=cons5pct_15_1961)
replace indicator = "pct_50_1961" if (ref_year==1961 & cons5>=cons5pct_45_1961 & cons5<=cons5pct_55_1961)
replace indicator = "pct_90_1961" if (ref_year==1961 & cons5>=cons5pct_85_1961 & cons5<=cons5pct_95_1961)
gen str12 ind_fdhome=""
replace ind_fdhome= "pct_10_1961" if (ref_year==1961 & fdhome_scle>=fdhomepct_5_1961 & fdhome_scle<=fdhomepct_15_1961)
replace ind_fdhome= "pct_50_1961" if (ref_year==1961 & fdhome_scle>=fdhomepct_45_1961 & fdhome_scle<=fdhomepct_55_1961)
replace ind_fdhome= "pct_90_1961" if (ref_year==1961 & fdhome_scle>=fdhomepct_85_1961 & fdhome_scle<=fdhomepct_95_1961)

drop cons5pct_*
drop fdhomepct_*
*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.)

*Restrict sample to observations in a given year between percentiles 5 and 15, 45 and 55, and 85 and 95
preserve 
keep if indicator=="pct_10_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save percentile_means_1961 , replace 
restore 

preserve 
keep if ind_fdhome=="pct_10_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_50_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=50
save "`temp50'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_90_1961"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=90

append using `temp50'
append using `temp10'
save fdpct_means_1961
restore 

use percentile_means_allyears , clear 
append using percentile_means_1961
save percentile_means_allyears , replace 

use fdpct_means_allyears , clear 
append using fdpct_means_1961 
save fdpct_means_allyears , replace 

*1980s
use inequality_data_for_80 , clear 
drop if (ref_year==1983 | ref_year==1984)

*Create indicators of percentile of core consumption by year
****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot

*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1970)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale 

*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}
 
*Create indicators for "nearness" to percentiles of cons5
foreach year of numlist 1980/1981 {
foreach num of numlist 5 15 45 55 85 95 {
egen cons5pct_`num'_`year' = pctile(cons5) if (ref_year==`year') , p(`num')
egen fdhomepct_`num'_`year' = pctile(fdhome_scle) if (ref_year==`year') , p(`num') 
}
}
gen str12 indicator=""
foreach year of numlist 1980/1981 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & cons5>=cons5pct_5_`year' & cons5<=cons5pct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & cons5>=cons5pct_45_`year' & cons5<=cons5pct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & cons5>=cons5pct_85_`year' & cons5<=cons5pct_95_`year')
}
gen str12 ind_fdhome=""

foreach year of numlist 1980/1981 {
replace ind_fdhome= "pct_10_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_5_`year' & fdhome_scle<=fdhomepct_15_`year')
replace ind_fdhome= "pct_50_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_45_`year' & fdhome_scle<=fdhomepct_55_`year')
replace ind_fdhome= "pct_90_`year'" if (ref_year==`year' & fdhome_scle>=fdhomepct_85_`year' & fdhome_scle<=fdhomepct_95_`year')
}
drop fdhomepct_*
drop cons5pct_*

*Create indicator for house ownership
gen home_own = 0
replace home_own = 1 if (ref_year==1961 & cutenure==1) 
replace home_own = 1 if (ref_year>=1970 & ref_year!=. & (cutenure==1 | cutenure==2 | cutenure==3))
*create indicator for cars that will be consistent for all years 
gen vehq_cons = 0 if (vehq==0) 
replace vehq_cons = 1 if (vehq==1)
replace vehq_cons = 2 if (vehq>=2 & vehq!=.)

*Restrict sample to observations in a given year between percentiles 5 and 15, 45 and 55, and 85 and 95

foreach year of numlist 1980/1981 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save percentile_means_`year' , replace 
restore 

preserve 
keep if ind_fdhome=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=50
save "`temp50'" , replace 
restore

preserve 
keep if ind_fdhome=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq vehq_cons home_own [w=wgt20] , by (ref_year)
gen percentile=90

append using `temp50'
append using `temp10'
save fdpct_means_`year' , replace 
restore
}

use percentile_means_allyears , clear 
foreach year of numlist 1980/1981 {
append using percentile_means_`year' 
}
sort ref_year percentile 
save percentile_means_allyears , replace 
export excel using component_pctiles.xls , sh(comp_means) firstrow(var) 
use fdpct_means_allyears , clear 
foreach year of numlist 1980/1981 { 
append using fdpct_means_`year'
}
sort ref_year percentile 
save fdpct_means_allyears , replace 
export excel using component_pctiles.xls , sh(fdpct_means) firstrow(var) 

***REPEAT ANALYSIS FOR 1980Q1-1980Q3 AND 1981Q1-1981Q3
*ONLY keep 3 quarters of 1980
use inequality_data_for_80 , clear 
drop if (ref_year==1983 | ref_year==1984)

keep if ((qyear>=801 & qyear<=803) | (qyear>=810 & qyear<=813))

*Create indicators of percentile of core consumption by year
****CREATE COMPONENT PERCENTILES 
*Create transportation total variable with ONLY transportation expenditures included in core consumption measures
gen trans_tot1=.
replace trans_tot1 = (n_tot_vflow1/4)+gas_mo if (ref_year<=1979)
replace trans_tot1 = n_tot_vflow1+gas_mo if (ref_year>=1980)

*create housing + utilities expenditures measure 
gen hous_util_tot = hous_tot+util_tot

*create vehicle total
gen vehic_tot =.
replace vehic_tot = n_tot_vflow1/4 if (ref_year<=1970)
replace vehic_tot = n_tot_vflow1 if (ref_year>=1980)

*scale all component measures 
gen trans_scle = trans_tot1/scale
gen hous_scle = hous_tot/scale
gen fdhome_scle = fdhome_tot/scale 
gen hous_util_scle = hous_util_tot/scale 
gen vehic_scle = vehic_tot/scale 

*convert to 2011 dollars 
foreach var of varlist trans_scle hous_scle fdhome_scle hous_util_scle vehic_scle {
gen r_`var'=cpi_u_rs_adj_11*`var' 
}
 
foreach year of numlist 1980/1981 {
foreach num of numlist 5 15 45 55 85 95 {
egen cons5pct_`num'_`year' = pctile(cons5) if (ref_year==`year') , p(`num') 
}
}
gen str12 indicator=""
foreach year of numlist 1980/1981 {
replace indicator = "pct_10_`year'" if (ref_year==`year' & cons5>=cons5pct_5_`year' & cons5<=cons5pct_15_`year')
replace indicator = "pct_50_`year'" if (ref_year==`year' & cons5>=cons5pct_45_`year' & cons5<=cons5pct_55_`year')
replace indicator = "pct_90_`year'" if (ref_year==`year' & cons5>=cons5pct_85_`year' & cons5<=cons5pct_95_`year')
}
drop cons5pct_*
*Restrict sample to observations in a given year between percentiles 5 and 15, 45 and 55, and 85 and 95
tempfile temp1980 temp1981 

foreach year of numlist 1980/1981 { 

preserve 
keep if indicator=="pct_10_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq [w=wgt20] , by (ref_year)
gen percentile=10
save "`temp10'" , replace 
restore 

preserve 
keep if indicator=="pct_50_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq [w=wgt20] , by(ref_year)
gen percentile=50
save "`temp50'" , replace 
restore 

preserve 
keep if indicator=="pct_90_`year'"
collapse r_trans_scle r_hous_scle r_fdhome_scle r_hous_util_scle r_vehic_scle vehq [w=wgt20] , by(ref_year)
gen percentile=90

append using `temp50' 
append using `temp10'
save "`temp`year''" , replace 
restore 
}
use "`temp1980'" , clear
append using "`temp1981'" 
sort ref_year percentile 
save percentile_means_8081q1q3 , replace 
export excel using component_pctiles.xls , sh(comp_q1q3) firstrow(var) 


